﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace DEMI_webapp
{
    public partial class PrzyjeciaMagazynowe : System.Web.UI.Page
	{
        System.Configuration.ConnectionStringSettings connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"];
        SqlConnection con;
        SqlDataReader reader;

		protected void Page_Load (object sender, EventArgs e)
		{
            if (!IsPostBack)
            {             
                Calendar1.SelectedDate = DateTime.Today;

                con = new SqlConnection(connString.ConnectionString);
                con.Open();
                SqlCommand magazynierzy = new SqlCommand("select * from Magazynierzy", con);
                reader = magazynierzy.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("imie"));
                    p += " " + reader.GetString(reader.GetOrdinal("nazwisko"));
                    List_Magazynier.Items.Add(p);
                }
                reader.Close();

                SqlCommand kapelusze = new SqlCommand("select distinct nazwa from kapelusze", con);
                reader = kapelusze.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("nazwa"));
                    List_Nazwa.Items.Add(p);
                }
                reader.Close();

                SqlCommand kolory = new SqlCommand("select kolor from Kapelusze where nazwa= @Nazwa", con);
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Nazwa";
                param.Value = List_Nazwa.SelectedItem.Text;
                kolory.Parameters.Add(param);
                reader = kolory.ExecuteReader();
                while (reader.Read())
                {
                    string p = reader.GetString(reader.GetOrdinal("kolor"));
                    List_Kolor.Items.Add(p);
                }
                reader.Close();
                con.Close();
            }   
		}

        protected void List_Nazwa_SelectedIndexChanged(object sender, EventArgs e)
        {
            con = new SqlConnection(connString.ConnectionString);
            con.Open();
            List_Kolor.Items.Clear();
            SqlCommand kolory = new SqlCommand("select kolor from Kapelusze where nazwa= @Nazwa", con);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@Nazwa";
            param.Value = List_Nazwa.SelectedItem.Text;
            kolory.Parameters.Add(param);
            reader = kolory.ExecuteReader();
            while (reader.Read())
            {
                string p = reader.GetString(reader.GetOrdinal("kolor"));
                List_Kolor.Items.Add(p);
            }

            con.Close();
        }

		protected void Button_Przyjecie_Click (object sender, ImageClickEventArgs e)
        {
            con = new SqlConnection(connString.ConnectionString);
            con.Open();
            
            SqlCommand magazynier = new SqlCommand("select id from Magazynierzy where imie= @Imie and nazwisko=@Nazwisko" , con);
            string[] dane = List_Magazynier.SelectedItem.Text.Split(' ');
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@Imie";
            param.Value = dane[0];
            magazynier.Parameters.Add(param);
            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@Nazwisko";
            param1.Value = dane[1];
            magazynier.Parameters.Add(param1);
            int id_magazyniera = (Int32)magazynier.ExecuteScalar();

            SqlCommand kapelusz = new SqlCommand("select id from Kapelusze where nazwa= @Nazwa and kolor=@Kolor", con);
           
            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@Nazwa";
            param2.SqlDbType = SqlDbType.VarChar;
            param2.Value = List_Nazwa.SelectedItem.Text;
            kapelusz.Parameters.Add(param2);
            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@Kolor";
            param3.SqlDbType = SqlDbType.VarChar;
            param3.Value = List_Kolor.SelectedItem.Text;
            kapelusz.Parameters.Add(param3);
            int id_kapelusza = (Int32)kapelusz.ExecuteScalar();

            SqlCommand insert = new SqlCommand("INSERT INTO Historia VALUES (1,@Data,@Id_mag,@Ilosc,@Id_kap,null)", con);
            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@Data";
            param4.SqlDbType = SqlDbType.SmallDateTime;
            param4.Value = Calendar1.SelectedDate;
            insert.Parameters.Add(param4);

            SqlParameter param5 = new SqlParameter();
            param5.ParameterName = "@Id_mag";
            param5.SqlDbType = SqlDbType.Int;
            param5.Value = id_magazyniera;
            insert.Parameters.Add(param5);

            SqlParameter param6 = new SqlParameter();
            param6.ParameterName = "@Ilosc";
            param6.SqlDbType = SqlDbType.Int;
            param6.Value = List_Ilosc.SelectedItem.Text;
            insert.Parameters.Add(param6);

            SqlParameter param7 = new SqlParameter();
            param7.ParameterName = "@Id_kap";
            param7.SqlDbType = SqlDbType.Int;
            param7.Value = id_kapelusza;
            insert.Parameters.Add(param7);

            insert.ExecuteNonQuery();

            SqlCommand select_ilosc = new SqlCommand("select ilosc from Kapelusze where nazwa= @Nazwa and kolor=@Kolor", con);
            SqlParameter param9 = new SqlParameter();
            param9.ParameterName = "@Nazwa";
            param9.SqlDbType = SqlDbType.VarChar;
            param9.Value = List_Nazwa.SelectedItem.Text;
            select_ilosc.Parameters.Add(param9);
            SqlParameter param10 = new SqlParameter();
            param10.ParameterName = "@Kolor";
            param10.SqlDbType = SqlDbType.VarChar;
            param10.Value = List_Kolor.SelectedItem.Text;
            select_ilosc.Parameters.Add(param10);
            int ilosc = (Int32)select_ilosc.ExecuteScalar();

            int nowa_ilosc = ilosc + Convert.ToInt32(List_Ilosc.SelectedItem.Text);

            SqlCommand update_ilosc = new SqlCommand("UPDATE Kapelusze SET [ilosc] = @Ilosc WHERE id = @Id_kap", con);
            SqlParameter param8 = new SqlParameter();
            param8.ParameterName = "@Ilosc";
            param8.SqlDbType = SqlDbType.Int;
            param8.Value = nowa_ilosc;
            update_ilosc.Parameters.Add(param8);
            SqlParameter param11 = new SqlParameter();
            param11.ParameterName = "@Id_kap";
            param11.SqlDbType = SqlDbType.Int;
            param11.Value = id_kapelusza;
            update_ilosc.Parameters.Add(param11);

            update_ilosc.ExecuteNonQuery();

            Response.Write("<script>alert('Przyjęto do magazynu')</script>");
            con.Close();
        }
	}
}